<?php

namespace App\Services;


use App\Models\Cms_Evaluate;
use App\Models\Cms_Store;
use App\Models\Sys_User;
use AlibabaCloud\SDK\Dysmsapi\V20180501\Dysmsapi;
use AlibabaCloud\Tea\Exception\TeaError;
use \Exception;
use AlibabaCloud\Tea\Utils\Utils;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Dysmsapi\V20180501\Models\SendMessageToGlobeRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;

class EvaluateService
{
    public function getList($params)
    {
        $result = Cms_Evaluate::getFilter($params)
            ->orderBy('created_at','desc')
            ->paginate($params['pageSize']);
        $list['list'] = $result->items();
        $list['total'] = $result->total();
        return $list;
    }

    public function getInfo($params)
    {
        return Cms_Evaluate::find($params['id']);
    }

    public static function createClient(){
        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考。
        // 建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/311677.html。
        $config = new Config([
            // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
            "accessKe·yId" => getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
            // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
            "accessKeySecret" => getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
        ]);
        // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
        $config->endpoint = "dysmsapi.ap-southeast-1.aliyuncs.com";
        return new Dysmsapi($config);
    }

    public static function sendMessageToGlobe()
    {
        $thirdData = Cms_Evaluate::where('status','Y')
            ->where('created_at', '<', now()->subDays(3))
            ->get(['id','phone']);
        $client = self::createClient();

        foreach ($thirdData as $v){
            $sendMessageToGlobeRequest = new SendMessageToGlobeRequest([
                "to" => $v->phone,
                "message" => "Dear Customer, here is the survey link from Jade Nails & Spa (#https://jnsny-h5.orchidgardenspa.com/#/pages/survey/index?eid=".$v->id."). Please click the link to complete the survey. Thank you for your time!",
                "from" => "18338580873"  //Sender ID
            ]);

            try {
                // 复制代码运行请自行打印 API 的返回值
                $client->sendMessageToGlobeWithOptions($sendMessageToGlobeRequest, new RuntimeOptions([]));
            } catch (Exception $error) {
                if (!($error instanceof TeaError)) {
                    $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
                }
                // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
                // 错误 message
                Utils::assertAsString($error->message);
            }
        }

        $fourteenData = Cms_Evaluate::where('status','Y')
            ->where('created_at', '<', now()->subDays(14))
            ->get(['id','phone']);
        foreach ($fourteenData as $v){
            $sendMessageToGlobeRequest = new SendMessageToGlobeRequest([
                "to" => $v->phone,
                "message" => "Dear Customer, here is the survey link from Jade Nails & Spa (#https://jnsny-h5.orchidgardenspa.com/#/pages/survey/index1?eid=".$v->id."). Please click the link to complete the survey. Thank you for your time!",
                "from" => "18338580873"  //Sender ID
            ]);

            try {
                // 复制代码运行请自行打印 API 的返回值
                $client->sendMessageToGlobeWithOptions($sendMessageToGlobeRequest, new RuntimeOptions([]));
            } catch (Exception $error) {
                if (!($error instanceof TeaError)) {
                    $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
                }
                // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
                // 错误 message
                Utils::assertAsString($error->message);
            }
        }

    }

    public function evaluateAdd($params)
    {
        $store = Cms_Store::find($params['storeid']);
        $params['storename'] = $store->name;
        $staff = Sys_User::find($params['staffid']);
        $params['staffname'] = $staff->nickname;
        return Cms_Evaluate::create($params);
    }

    public function evaluateAddThird($params)
    {
        $eval = Cms_Evaluate::find($params['eid']);
        $eval->thirdQ1 = $params['q1'];
        $eval->thirdQ2 = $params['q2'];
        $eval->thirdQ3 = $params['q3'];
        return $eval->save();
    }

    public function evaluateAddFourteen($params)
    {
        $eval = Cms_Evaluate::find($params['eid']);
        $eval->fourteenQ1 = $params['q1'];
        $eval->fourteenQ2 = $params['q2'];
        $eval->fourteenQ3 = $params['q3'];
        return $eval->save();
    }

}
